<template>
  <div class="data-table-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <el-form ref="queryFormRef" :model="state.tableData.param.params" inline>
        <el-form-item class="!mr-4" prop="barcode">
          <el-input v-model="state.tableData.param.params.barcode"
                    placeholder="请输入商品条码" size="default"/>
        </el-form-item>
        <el-form-item class="!mr-4" prop="name">
          <el-input v-model="state.tableData.param.params.name"
                    placeholder="请输入商品名称" size="default"/>
        </el-form-item>
        <el-form-item class="!mr-4" prop="category" style="width: 11%">
          <el-select v-model="state.tableData.param.params.category"
                     clearable
                     placeholder="请选择操作类型"
                     size="default">
            <el-option
                v-for="item in state.categoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="getTableData">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'user.add'" class="!mr-4">
          <el-button size="default" type="success" @click="onOpenAddUser('add')">
            <el-icon>
              <ele-FolderAdd/>
            </el-icon>
            新增
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="onHandleResetFields">
            <el-icon>
              <ele-Refresh/>
            </el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!--显示字段-->
      <el-row :gutter="10" class="mb8">
        <right-toolbar :columns="state.tableData.columns" @queryTable="getTableData"></right-toolbar>
      </el-row>

      <el-table v-loading="state.tableData.loading" :data="state.tableData.data" highlight-current-row
                style="width: 100%"
                @sort-change="handleSortChange">
        <el-table-column v-if="state.tableData.columns[0].visible" label="ID" prop="id" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[1].visible" label="条码" prop="barcode" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[2].visible" label="货号" prop="artNo" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[3].visible" label="商品类型" prop="type" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[4].visible" label="品牌名称" prop="brandName"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[5].visible" label="名称" prop="name" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[6].visible" label="分类" prop="categoryName"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[7].visible" label="规格" prop="specName" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[8].visible" label="单位" prop="unit" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[9].visible" label="成本价" prop="costPrice"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[10].visible" label="销售价" prop="salePrice"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[11].visible" label="会员价" prop="vipPrice"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[12].visible" label="划线价" prop="scribePrice"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[13].visible" label="热门产品" prop="isHot"
                         show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.isHot"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="state.tableData.columns[14].visible" label="是否积分" prop="isPoint"
                         show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.isPoint"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="state.tableData.columns[15].visible" label="积分值" prop="point" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[21].visible" label="是否换购" prop="isExchange"
                         show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.isExchange"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="state.tableData.columns[16].visible" label="换购积分" prop="exchangePoint"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[17].visible" label="提成类型" prop="commissionType"
                         show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="commission_type" :value="scope.row.commissionType"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column v-if="state.tableData.columns[18].visible" label="提成金额" prop="commissionAmount"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[19].visible" label="库存预警" prop="stockWarning"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[20].visible" label="备注" prop="remark"/>
        <el-table-column label="操作" width="250">
          <template #default="scope">
            <el-button v-auth="'user.edit'" :disabled="scope.row.loginName === 'admin'" size="small" text
                       type="primary" @click="onOpenEditUser('edit', scope.row)">
              修改
            </el-button>
            <el-button v-auth="'user.del'" :disabled="scope.row.loginName === 'admin'" size="small" text
                       type="danger" @click="onRowDel(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:current-page="state.tableData.param.pageNum"
                     v-model:page-size="state.tableData.param.pageSize" :page-sizes="[10, 20, 30]" :pager-count="5"
                     :total="state.tableData.total" background class="mt15"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange">
      </el-pagination>
    </el-card>
    <commodityDialog ref="commodityDialogRef" @refresh="getTableData()"/>
  </div>
</template>

<script lang="ts" name="systemUser" setup>
import {defineAsyncComponent, getCurrentInstance, onMounted, reactive, ref} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';
import {commodityApi} from "/@/api/store/commodity";
import {commodityCategoryApi} from "/@/api/store/commodity/category/commodity";
import DictTag from "/@/components/DictTag/index.vue";

const {proxy} = getCurrentInstance();
const {sys_yes_no, commission_type} = proxy.useDict("sys_yes_no", "commission_type");

// 引入组件
const commodityDialog = defineAsyncComponent(() => import('/src/views/store/commodity/dialog.vue'));

// 定义变量内容
const commodityDialogRef = ref();
const queryFormRef = ref();
const state = reactive<SysUserState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
      params: {
        name: '',
        barcode: '',
        category: '',
      },
    },
    columns: [
      {key: 0, label: `ID`, visible: true},
      {key: 1, label: `条码`, visible: true},
      {key: 2, label: `货号`, visible: false},
      {key: 3, label: `商品类型`, visible: false},
      {key: 4, label: `品牌名称`, visible: true},
      {key: 5, label: `名称`, visible: true},
      {key: 6, label: `分类`, visible: true},
      {key: 7, label: `规格`, visible: false},
      {key: 8, label: `单位`, visible: false},
      {key: 9, label: `成本价`, visible: true},
      {key: 10, label: `销售价`, visible: true},
      {key: 11, label: `会员价`, visible: true},
      {key: 12, label: `划线价`, visible: false},
      {key: 13, label: `热门产品`, visible: true},
      {key: 14, label: `是否积分`, visible: false},
      {key: 15, label: `积分值`, visible: false},
      {key: 16, label: `换购积分`, visible: false},
      {key: 17, label: `提成类型`, visible: false},
      {key: 18, label: `提成金额`, visible: false},
      {key: 19, label: `库存预警`, visible: true},
      {key: 20, label: `备注`, visible: false},
      {key: 21, label: `是否换购`, visible: false},
    ],
  },
  categoryList: [],
});


const handleSortChange = (data: any) => {
  if (data.order) {
    if (data.order.indexOf('asc') > -1) {
      state.tableData.param.sortOrder = 'asc'
    } else {
      state.tableData.param.sortOrder = 'desc'
    }
    state.tableData.param.sortName = data.prop
  }
  getTableData();
};

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  commodityApi()
      .getTableData(state.tableData.param)
      .then((res) => {
        state.tableData.data = res.data.records;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
      })
      .catch(() => {
        state.tableData.loading = false;
      });
};
// 打开新增用户弹窗
const onOpenAddUser = (type: string) => {
  commodityDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, row: RowUserType) => {
  commodityDialogRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row: RowUserType) => {
  ElMessageBox.confirm(`此操作将永久删除账户“${row.userName}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    commodityApi()
        .delRecord(row)
        .then(() => {
          getTableData();
          ElMessage.success('删除成功');
        });
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();

  // 查询分类
  commodityCategoryApi()
      .findAll()
      .then(res => {
        state.categoryList = res.data;
      })
});
// 重置查询表单
const onHandleResetFields = () => {
  queryFormRef.value.resetFields();
  getTableData();
};
</script>
